home *** CD-ROM | disk | FTP | other *** search
- /**-------------------------------------------------------------------------------------
- --
- -- A very simple printing loop template
- --
- **-----------------------------------------------------------------------------------**/
-
- #include "Printing.h"
- #include <QDOffscreen.h>
-
-
- void PrintStuff (void);
- void DrawStuff (Rect theWorld);
- void PrepareOffScreens(void);
- void KillOffscreens(void);
-
- /*------ main ----------------------------------------------------------------------------*/
-
- CGrafPtr offscreen1, offscreen2;
- RgnHandle textRegion;
- Rect boundsRect;
-
- main()
-
- {
-
- GrafPort myPort;
-
- InitGraf((Ptr) &thePort);
- OpenPort(&myPort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- PrepareOffScreens();
- PrintStuff ();
- KillOffscreens();
-
- } /* main */
-
-
- /*------ PrepareOffScreens ---------------------------------------------------------------*/
-
- void PrepareOffScreens()
-
- {
-
- OSErr myErr;
- CGrafPtr oldCPort;
- GDHandle oldGD;
- short fontNum;
- PixPatHandle ourPixPat;
-
- SetRect(&boundsRect,0,0,700,700);
- GetGWorld(&oldCPort,&oldGD);
-
- /* create offscreen #1 */
-
- myErr = NewGWorld(&offscreen1, 1, &boundsRect, NULL, NULL, 0);
- if (myErr != noErr) {
- DebugStr("\pCouldn't create GWorld #1");
- ExitToShell();
- }
-
- SetGWorld(offscreen1,NULL);
- ClipRect(&boundsRect);
-
- ForeColor(blackColor);
- EraseRect(&boundsRect);
- GetFNum("\pTimes",&fontNum);
- TextFont(fontNum);
- TextFace(bold);
- TextSize(60);
- MoveTo(10,100);
- DrawString("\pHere’s our string test");
-
- /* OK, offscreen #1 now has our string in it. Let's make a region from it. */
-
- textRegion = NewRgn();
- myErr = BitMapToRegion(textRegion,&((GrafPtr)offscreen1)->portBits);
- if ((myErr != noErr) || ((**textRegion ).rgnSize == 10)) {
- DebugStr("\pCouldn't convert text to region");
- ExitToShell();
- }
-
- /* create offscreen #2 */
-
- myErr = NewGWorld(&offscreen2, 4, &boundsRect, NULL, NULL, 0);
- if (myErr != noErr) {
- DebugStr("\pCouldn't create GWorld #2");
- ExitToShell();
- }
-
- /* Fill it with a color pattern so we can see what's going on */
-
- SetGWorld(offscreen2,NULL);
- ClipRect(&boundsRect);
-
- ourPixPat = GetPixPat(16);
- FillCRect(&boundsRect,ourPixPat);
- DisposePixPat(ourPixPat);
-
- SetGWorld(oldCPort,oldGD);
-
- }
-
- /*------ DrawStuff -----------------------------------------------------------------------*/
-
- void DrawStuff (theRect)
-
- Rect theRect;
-
- {
- GrafPtr currentPort;
- PixMapHandle offscreenPix;
-
- /* Here's where we use CopyBits to copy from offscreen2 through the mask region */
-
- GetPort(¤tPort);
- offscreenPix = GetGWorldPixMap(offscreen2);
- LockPixels(offscreenPix);
- DebugStr("\pJust about to CopyBits...");
- CopyBits((BitMap*)*offscreenPix,
- &(currentPort)->portBits,
- &boundsRect, &boundsRect, srcCopy, textRegion);
- UnlockPixels(offscreenPix);
-
-
- } /* DrawStuff */
-
-
-
- /*------ PrintStuff ----------------------------------------------------------------------*/
-
- void PrintStuff ()
- {
- GrafPtr oldPort;
- THPrint thePrRecHdl;
- TPPrPort thePrPort;
- TPrStatus theStatus;
-
- GetPort(&oldPort);
-
- thePrRecHdl = (THPrint) NewHandle (sizeof (TPrint));
-
- PrOpen();
-
- if (PrError() == noErr)
- {
- PrintDefault(thePrRecHdl);
-
- PrValidate(thePrRecHdl);
-
- if (! PrStlDialog(thePrRecHdl))
- ExitToShell();
-
- if (! PrJobDialog(thePrRecHdl))
- ExitToShell();
-
- thePrPort = PrOpenDoc(thePrRecHdl, nil, nil);
-
- if (PrError() == noErr)
- {
- PrOpenPage(thePrPort, nil);
- if (PrError() == noErr)
- {
- /********* Print from here ****************/
-
- DrawStuff ((**thePrRecHdl).prInfo.rPage);
- }
- }
- PrClosePage(thePrPort);
- }
- PrCloseDoc(thePrPort);
-
- if ((((TPPrint)*thePrRecHdl)->prJob.bJDocLoop == bSpoolLoop) && (PrError() == noErr))
- PrPicFile(thePrRecHdl, nil, nil, nil, &theStatus);
-
- PrClose();
-
- SetPort(oldPort);
- } /* PrintStuff */
-
- /*------ KillOffscreens ------------------------------------------------------------------*/
-
- void KillOffscreens()
-
- {
- DisposeGWorld(offscreen1);
- DisposeGWorld(offscreen2);
- DisposeRgn(textRegion);
-
- }